//
//  DesDetailViewController.m
//  穷游
//
//  Created by lanou3g on 15/12/19.
//  Copyright © 2015年 王忠奎. All rights reserved.
//

#import "DesDetailViewController.h"
#import "DesModel.h"
#import "DesDataHandle.h"
#import "DesDetailCollectionViewCell.h"
@interface DesDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *collection;


@property(nonatomic,strong)NSDictionary *dict;

@end

@implementation DesDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title = [DesDataHandle sharedDesDataHandle].key;
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = left;

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(157, 250);
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    self.collection = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    [self.view addSubview:self.collection];
    
    //设置代理
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
   
    //注册cell
    [self.collection registerNib:[UINib nibWithNibName:@"DesDetailCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"DesDetailCollectionViewCell"];
    self.collection.backgroundColor = [UIColor whiteColor];
    
    

    //刷新
    [[DesDataHandle sharedDesDataHandle] getDesDataFromNet:^(id isFinished, id dict) {
      
        self.dict = dict;
//         NSLog(@"+++++++++++%@",self.dict);
        [self.collection reloadData];
        
    }];
   
}

#pragma mark - 点击方法---leftItem
-(void)leftItemAction:(UIBarButtonItem *)sender
{

    [self.navigationController popViewControllerAnimated:YES];

}




#pragma mark ---实现代理方法

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return 1;


}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSString *key = [DesDataHandle sharedDesDataHandle].key;
//    NSLog(@"------++------%@",key);
    
    NSArray *array = [self.dict valueForKey:key];
//    NSLog(@"====++=====%@",[self.dict valueForKey:key]);
    //NSLog(@"%@",[DesDataHandle sharedDesDataHandle].desDataDict);
    return array.count;


}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    DesDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DesDetailCollectionViewCell" forIndexPath:indexPath];
    DesModel *model = [self.dict valueForKey:[DesDataHandle sharedDesDataHandle].key][indexPath.row ];
   [cell setCellDataWithModel:model];
    
  
    return cell;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
